﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using ServBus.ListForm2010.Setup.Utilities;

namespace ServBus.ListForm2010.Setup.Commands
{
    class DeactivateFeatureCmd : BaseCommand
    {
        public override string Description
        {
            get { return Properties.Resources.CmdDeactivateFeature; }
        }

        public override bool AllowRollback
        {
            get
            {
                return false;
            }
        }

        public override bool Execute()
        {
            try
            {
                Log.WriteLog(Log.LogType.Info, "Start Deactivating Feature...");
                foreach(SPWebApplication adminWebapp in SPWebService.AdministrationService.WebApplications)
                {
                    if(adminWebapp.Sites.Count == 0) continue;
                    using(SPSite site = adminWebapp.Sites[0])
                    {
                        site.RootWeb.Features.Remove(Constants.ADMIN_FEATURE_ID, true);
                    }
                }
                Log.WriteLog(Log.LogType.Info, "Feature Deactivated.");
                return true;
            }
            catch (Exception ex)
            {
                Log.WriteLog(Log.LogType.Error, "Deactivate Feature Error. " + ex.GetType().FullName + ". " + ex.Message);
                return true;
            }
        }

        public override bool Rollback()
        {
            return true;
        }
    }
}
